package dao

import (
	"yundian/internel/app/models/ticket"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

type TicketDao struct {
}

func NewTicketDaoDao() *TicketDao {
	return &TicketDao{}
}

func (dao *TicketDao) CreateTicketOrders(param *ticket.OrderDb) *dingo.ErrorWrap {
	db := global.Orm.Create(&param)
	return dingo.ErrorIf(db.Error, param)
}

func (dao *TicketDao) UpdateTicketOrders(param *ticket.OrderModel) *dingo.ErrorWrap {
	dbInfo := map[string]interface{}{}

	if param.ThirdTradeNo != "" {
		dbInfo["third_trade_no"] = param.ThirdTradeNo
	}

	if param.Status > 0 {
		dbInfo["status"] = param.Status
	}

	if param.PayTime != "" {
		dbInfo["pay_time"] = param.PayTime
	}

	db := global.Orm.Model(&param).Where("trade_no=?", param.TradeNo).Updates(dbInfo)
	return dingo.ErrorIf(db.Error, param)
}
